package br.edu.ufcg.mdaveritas.parser.input;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;


import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import br.edu.ufcg.mdaveritas.net.Arc;
import br.edu.ufcg.mdaveritas.net.Net;
import br.edu.ufcg.mdaveritas.net.NetObject;
import br.edu.ufcg.mdaveritas.net.Node;

public class PTNetParser {

	private List<Integer> parseObjects(String objects) {
		if (objects.equals("")) return new ArrayList<Integer>();
		List<Integer> indexes = new ArrayList<Integer>(); 
		String temp = objects.replace("//@pages.0/@objects.", "");
		String[] objArray = temp.split(" ");
		for (int i = 0; i < objArray.length; i++) {
			indexes.add(Integer.parseInt(objArray[i]));
		}
		return indexes;
	}
	
	private List<NetObject> getObjects(Element pn) {
		List<NetObject> objects = new ArrayList<NetObject>();
		NodeList netNodes = pn.getElementsByTagName("objects");
		
		for (int i = 0; i < netNodes.getLength(); i++) {
			Element obj = (Element) netNodes.item(i);
			if (obj.getAttribute("xsi:type").equals("ptnet:Place") ||
					obj.getAttribute("xsi:type").equals("ptnet:Transition")) {
				Element t = (Element) obj.getElementsByTagName("name").item(0);
				Node n = new Node(t.getAttribute("text"));
				n.setIsPlace(obj.getAttribute("xsi:type").equals("ptnet:Place"));
				n.setSourceIds(parseObjects(obj.getAttribute("InArcs")));
				n.setTargetIds(parseObjects(obj.getAttribute("OutArcs")));
				objects.add(n);
			} else {
				Arc arc = new Arc();
				arc.setSourceIds(parseObjects(obj.getAttribute("source")));
				arc.setTargetIds(parseObjects(obj.getAttribute("target")));
				objects.add(arc);
			}
		}
		return objects;
	}
	
	protected Net getPetriNet(String filepath) {
		File file = new File(filepath);
		Net petriNet = new Net();
		try {
			List<NetObject> objects = new ArrayList<NetObject>();
			
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db;
			db = dbf.newDocumentBuilder();
			Document doc = db.parse(file);
			doc.getDocumentElement().normalize();

			NodeList nodeLst = doc.getElementsByTagName("pages");
			Element pn;
			for (int i = 0; i < nodeLst.getLength(); i++) {
				pn = (Element) nodeLst.item(i);
				objects = getObjects(pn);
			}
			
			petriNet.setObjects(objects);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return petriNet;
	}

	protected Net initializePreAndPosSet(Net net) {
		Net n = net;
		List<NetObject> objects = n.getObjects();
		for (int i = 0; i < objects.size(); i++) {
			NetObject obj = objects.get(i);
			if (obj instanceof Node) {
				for (Integer id : obj.getSourceIds()) {
					Arc a = (Arc) n.getObjects().get(id);
					((Node) n.getObjects().get(i)).getPreSet().add((Node) n.getObjects().get(a.getSourceIds().get(0)));
				}
				for (Integer id : obj.getTargetIds()) {
					Arc a = (Arc) n.getObjects().get(id);
					((Node) n.getObjects().get(i)).getPosSet().add((Node) n.getObjects().get(a.getTargetIds().get(0)));					
				}
			} else {
				Integer id = obj.getSourceIds().get(0);
				((Arc) n.getObjects().get(i)).setSource((Node) n.getObjects().get(id));
				id = obj.getTargetIds().get(0);
				((Arc) n.getObjects().get(i)).setTarget((Node) n.getObjects().get(id));
			}
		}
		return n;
	}
}
